home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / DefaultCellRenderer.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  245 lines

  1. /*
  2.  * @(#)DefaultCellRenderer.java    1.17 98/01/12
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.awt.Component;
  24. import java.awt.Color;
  25. import java.lang.Boolean;
  26. import com.sun.java.swing.table.*;
  27. import com.sun.java.swing.*;
  28. import java.io.Serializable;
  29.  
  30. /**
  31.  * @version 1.17 01/12/98
  32.  * @author Hans Muller
  33.  * @author Alan Chung
  34.  * @see JTable
  35.  */
  36.  
  37. public class DefaultCellRenderer implements TableCellRenderer, Serializable {
  38.  
  39.     // PENDING(alan): Should use system selection color
  40.     protected final static Color selectionColor = new Color(0,0,128);
  41.  
  42. //
  43. // Instance Variables
  44. //
  45.  
  46.     protected JComponent component;
  47.     protected ValueProperty value;
  48.  
  49.     protected Color backgroundColor;
  50.     protected Color foregroundColor;
  51.     protected Color selectedBackgroundColor;
  52.     protected Color selectedForegroundColor;
  53.  
  54. //
  55. // Constructors
  56. //
  57.  
  58.     public DefaultCellRenderer(JLabel x) {
  59.         this.component = x;
  60.     x.setOpaque(true);
  61.         this.value = new ValueProperty() {
  62.             public void setValue(Object x) {
  63.         // Set value to empty string so it will display a
  64.         // blank cell, and not cause an exception
  65.         if (x == null)
  66.             x = "";
  67.  
  68.                 super.setValue(x);
  69.         if (x instanceof Icon)
  70.             ((JLabel)component).setIcon((Icon)x);
  71.         else
  72.             ((JLabel)component).setText(x.toString());
  73.             }
  74.         };
  75.  
  76.     // Default label colors
  77.     /*
  78.     setBackgroundColor(Color.white);
  79.     setForegroundColor(Color.black);
  80.     setSelectedBackgroundColor(selectionColor);
  81.     setSelectedForegroundColor(Color.white);
  82.     */
  83.     }
  84.  
  85.     public DefaultCellRenderer(JButton x) {
  86.         this.component = x;
  87.         this.value = new ValueProperty() {
  88.             public void setValue(Object x) {
  89.         // Set value to empty string so it will display a
  90.         // blank button, and not cause an exception
  91.         if (x == null)
  92.             x = "";
  93.  
  94.                 super.setValue(x);
  95.                 ((JButton)component).setText(x.toString());
  96.             }
  97.         };
  98.  
  99.     // Default button colors
  100.     /*
  101.     setBackgroundColor(Color.lightGray);
  102.     setForegroundColor(Color.black);
  103.     setSelectedBackgroundColor(Color.darkGray);
  104.     setSelectedForegroundColor(Color.white);
  105.         */
  106.     }
  107.  
  108.     public DefaultCellRenderer(JCheckBox x) {
  109.         this.component = x;
  110.         this.value = new ValueProperty() {
  111.             public void setValue(Object x) {
  112.         // Set value to empty string so it will not
  113.         // cause an exception
  114.         if (x == null)
  115.             x = "";
  116.  
  117.                 super.setValue(x);
  118.  
  119.         // Try my best to do the right thing with x
  120.         if (x instanceof Boolean) {
  121.             ((JCheckBox)component).setSelected(((Boolean)x).booleanValue());
  122.         }
  123.         else if (x instanceof String) {
  124.             Boolean b = new Boolean((String)x);
  125.             ((JCheckBox)component).setSelected(b.booleanValue());
  126.         }
  127.         else {
  128.             ((JCheckBox)component).setSelected(false);
  129.         }
  130.             }
  131.         };
  132.  
  133.     // Default checkbox colors
  134.     /*
  135.     setBackgroundColor(Color.white);
  136.     setForegroundColor(Color.black);
  137.     setSelectedBackgroundColor(selectionColor);
  138.     setSelectedForegroundColor(Color.white);
  139.     */
  140.     }
  141.  
  142. //
  143. // Modifying and Querying
  144. //
  145.  
  146.     public void setBackgroundColor(Color newColor) {
  147.     backgroundColor = newColor;
  148.     }
  149.  
  150.     public Color getBackgroundColor() {
  151.     return backgroundColor;
  152.     }
  153.  
  154.     public void setForegroundColor(Color newColor) {
  155.     foregroundColor = newColor;
  156.     }
  157.  
  158.     public Color getForegroundColor() {
  159.     return foregroundColor;
  160.     }
  161.  
  162.     public void setSelectedBackgroundColor(Color newColor) {
  163.     selectedBackgroundColor = newColor;
  164.     }
  165.  
  166.     public Color getSelectedBackgroundColor() {
  167.     return selectedBackgroundColor;
  168.     }
  169.  
  170.     public void setSelectedForegroundColor(Color newColor) {
  171.     selectedForegroundColor = newColor;
  172.     }
  173.  
  174.     public Color getSelectedForegroundColor() {
  175.     return selectedForegroundColor;
  176.     }
  177.  
  178.     public void setToolTipText(String text) {
  179.     if (component instanceof JComponent)
  180.         ((JComponent)component).setToolTipText(text);
  181.     }
  182.  
  183.     public Component getComponent() {
  184.     return component;
  185.     }
  186.  
  187. //
  188. // Implementing TableCellRenderer
  189. //
  190.  
  191.     public Component getTableCellRendererComponent(JTable table, Object value,
  192.                            boolean isSelected, boolean hasFocus,
  193.                            int row, int column) {
  194.         // PENDING(philip): Hacks for Motif L&F.
  195.     // The muddle of if clauses below are minimal hacks that were included
  196.     // to make the motif L&F table use the correct selection colors
  197.     // for the for the first motif L&F release.
  198.     // This all needs to be redone.
  199.     if (isSelected) {
  200.         if (selectedBackgroundColor == null) {
  201.             component.setBackground(UIManager.getColor("textHighlight"));
  202.         }
  203.         else {
  204.             component.setBackground(selectedBackgroundColor);
  205.         }
  206.         if (selectedForegroundColor == null) {
  207.             component.setForeground(UIManager.getColor("textHighlightText"));
  208.         }
  209.         else {
  210.             component.setForeground(selectedForegroundColor);
  211.         }
  212.     }
  213.     else {
  214.     //    if (backgroundColor != null) {
  215.             component.setBackground(backgroundColor);
  216.     //    } 
  217.     //    else {
  218.     //        component.setBackground(Color.white);
  219.     //    }
  220.     //    if (foregroundColor != null) { 
  221.             component.setForeground(foregroundColor);
  222.     //    }
  223.     //    else {
  224.     //        component.setForeground(Color.black);
  225.     //    }
  226.     }
  227.  
  228.         this.value.setValue(value);
  229.         return component;
  230.     }
  231.  
  232.  
  233.     protected class ValueProperty implements Serializable {
  234.         protected Object value;
  235.  
  236.         public void setValue(Object x) {
  237.             this.value = x;
  238.         }
  239.     }
  240.  
  241.  
  242. }
  243.  
  244.  
  245.